/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package beatbox;
import com.illposed.osc.*;
import java.util.Date;
import java.util.Observable;
import java.util.*;

/**Class that monitors 6448 for features.  When recieved, creates an example and
 * adds it to the queue.
 *
 * expects format as specified in extractor.
 *
 * @author Kyle
 */
public class FeatureListener extends Observable implements OSCListener {
    private Queue<Example> q;
    private boolean listen = false;
    public FeatureListener() {
        q = new LinkedList<Example>();
    }

    //Actually start storing and processing examples
    public void startListening() {
        listen = true;
    }

    public void stopListening() {
        listen = false;
    }

    //return number of examples still in queue
    public int examplesLeft() {
        return q.size();
    }

    @Override
    public void acceptMessage(Date date, OSCMessage oscm) {
        if (listen) {
            float[] vars = new float[9];
            for (int i = 0; i < 9; i++) {
                vars[i] = (Float) oscm.getArguments()[i];
            }
            q.add(new Example(vars, (Integer) oscm.getArguments()[9]));
            System.out.println(Arrays.toString(vars) + (Integer) oscm.getArguments()[9]);
            startNotification();
        }
    }
    
    /**
     * @requires hasExamples()
     * @return the values of the earliest received
     * un-got example.  Get's this one and removes from q
     */
    public Example getNextExample() {
        return q.remove();
    }

    /**
     * @return true iff q is empty
     */
    public boolean hasExamples() {
        return q.isEmpty();
    }

    public void startNotification() {
        setChanged();
        notifyObservers();
    }

}
